package clt.com.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import clt.com.action.CommonAction;
import clt.com.object.SysMenu;
import clt.com.object.User;
import clt.com.service.INewsService;
import clt.com.service.ISystemService;
import clt.com.service.IUserService;
import clt.com.util.DateUtil;


@RestController
public class SysController {
	@Autowired
	private Environment env;
	@Autowired
    private ISystemService systemService;
	/**
	 * 
	 */
	private static final long serialVersionUID = -3983037068878331518L;

	//@RequestMapping("/hello")
	@RequestMapping("/getUserById")
    public String index() {
        return "测3333v嘎嘎嘎试spring";
    }
	@Autowired
    private IUserService userService;

    /*@ResponseBody
    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public int addUser(User user){
        return userService.addUser(user);
    }
    @ResponseBody
    @RequestMapping(value = "/all/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"})
    public Object findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){

        return userService.findAllUser(pageNum,pageSize);
    }
    @RequestMapping(value="/getUsers",method = RequestMethod.GET)
    public List<User> getUsers() {
        return userService.getAll();
    }*/
    /*@RequestMapping(value="/getListUsers",method = RequestMethod.GET)
    public void getList(String userName,String aaaa,String bbbb) {
    	//Map<String,Object> param=CommonAction.convert(req);
    	//InputObject n = (InputObject) ServletActionContext.getContext().get(ControlConstants.INPUTOBJECT);
    	Map<String,String> map = new HashMap<>();
    	map.put("userName", userName);
    	//map.put("userName", aaaa);
    	//map.put("userName", bbbb);
    	userService.test(map);
    	//userService.getAll();
    	System.err.println("**************完成***********8");
       // return userService.test(map);
    }*/
    @RequestMapping(value="/uploadPicFile", method=RequestMethod.POST)  
    @ResponseBody
    public  Map<String,String> uploadPicFile(@RequestParam("uploadfile") MultipartFile file){ 
    	Map<String,String> map = new HashMap<>();
        if (!file.isEmpty()) {  
            try { 
               // String filename = file.getOriginalFilename();
    			//临时图片名称
    			int code = (int) ((Math.random() * 9 + 1) * 100000);
    			String tempName = DateUtil.date2String(new Date(),DateUtil.DATE_PATTERN.YYYYMMDDHHMMSS)+ code+".jpg";
                String directory = env.getProperty("jkd.paths.uploadedFiles");
                String filepath = Paths.get(directory, tempName).toString(); 
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(filepath)));
                stream.write(file.getBytes());
                stream.close();               
                map.put("picUrl", tempName);
                map.put("rtnMsg", "文件上传成功");
            } catch (Exception e) {  
            	map.put("picUrl", "");
                map.put("rtnMsg", "文件上传失败"); 
            }  
        } else {  
        	map.put("picUrl", "");
            map.put("rtnMsg", "文件上传失败");   
        }  
        map.put("busiCode", "");
    	map.put("returnCode", "0");
    	map.put("returnMessage", "");
    	map.put("object", "");
    	map.put("busiCode", ""); 
    	return map;
    }  
    /**
     * 新增菜单
     * @param sysMenu
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/saveUiInfo",method = RequestMethod.POST)
    public Map<String,Object> saveUiInfo(SysMenu sysMenu) throws Exception {
    	sysMenu.setCrtUserId("123456789");
    	sysMenu.setUiValidFlag("1");
        return systemService.saveMenuInfo(sysMenu);
    }
    /**
     * 新增菜单
     * @param queryUiList
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryUiList")
    public Map<String,Object> queryUiList(String limit ,String start) throws Exception {
    	Map<String,String> map = new HashMap<>();
    	map.put("start", start);
    	map.put("limit", limit);
        return systemService.queryMenuList(map);
    }
    /**
     * 首页菜单列表
     * @param queryMenuLeftTop
     * @return
     * @throws Exception
     */
    @RequestMapping("/homePageMenu")
    public Map<String,Object> queryMenuLeftTop() throws Exception {
    	Map<String,String> map = new HashMap<>();    	
        return systemService.queryMenuLeftTop(map);
    }
    /**
     * 编辑菜单
     * @param sysMenu
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/editUiInfo",method = RequestMethod.POST)
    public Map<String,Object> editUiInfo(SysMenu sysMenu) throws Exception {
        return systemService.updateMenuInfo(sysMenu);
    }
    /**
     * 删除菜单
     * @param sysMenu
     * @return
     * @throws Exception
     */
    @RequestMapping("/delUiInfo")
    public Map<String,Object> delUiInfo(SysMenu sysMenu) throws Exception {
        return systemService.delMenuInfo(sysMenu);
    }
}
